/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gob.inamhi.gui;

import gob.inamhi.entidades.Prpm;
import gob.inamhi.md.RawDataMD;
import inamhi.gob.dp.HiloTarea;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.quartz.CronScheduleBuilder;
import static org.quartz.JobBuilder.newJob;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

/**
 *
 * @author dhinojosa
 */
public class Procesador extends JPanel {

    private Scheduler sched;
    private SchedulerFactory sf;

    public Procesador() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }

    }

    private void agendarTareas() {
        try {
            RawDataMD est = new RawDataMD();
            ArrayList<String[]> parametros = est.getParametrosAProcesar();
            Iterator it = parametros.iterator();
            Properties props = new Properties();
            Integer numeroHilos = 200;
            props.put("org.quartz.threadPool.threadCount", numeroHilos.toString());
            sf = new StdSchedulerFactory(props);
            sched = sf.getScheduler();

            while (it.hasNext()) {
                //            new Thread(new HiloTarea((Integer) it.next())).start();
                String[] prpm = (String[]) it.next();
                int identificadorProceso = Integer.parseInt(prpm[0]);
                JobDetail demoJob = newJob(HiloTarea.class).build();
                demoJob.getJobDataMap().put("copa__id", prpm[1]);  //Código del parámetro a procesar
                demoJob.getJobDataMap().put("coparesu", prpm[2]);  //Código del parámetro resultante
                demoJob.getJobDataMap().put("prpmoper", prpm[3]);  //Tipo de operación matemática a realizarse sobre los datos
                demoJob.getJobDataMap().put("prpmdeci", prpm[4]);  //Intervalo de calculo del dato
                demoJob.getJobDataMap().put("prpmhorc", prpm[6]);  //Intervalo de calculo del dato
                demoJob.getJobDataMap().put("prpmpaso", prpm[7]);  //Intervalo de calculo del dato
                demoJob.getJobDataMap().put("prpmfnte", prpm[8]);  //Intervalo de calculo del dato
                demoJob.getJobDataMap().put("prpmdest", prpm[9]);  //Control de estado de la tarea
                demoJob.getJobDataMap().put("prpmesta", prpm[10]);  //Control de estado de la tarea
                demoJob.getJobDataMap().put("esta__id", prpm[11]);  //Control de estado de la tarea
                demoJob.getJobDataMap().put("prpmporc", prpm[12]);  //Control de estado de la tarea
                demoJob.getJobDataMap().put("cfges__id", prpm[13]);  //Control de estado de la tarea

                Trigger trigger = TriggerBuilder
                        .newTrigger()
                        .withIdentity("Proceso" + identificadorProceso, "group")
                        .withSchedule(
                        CronScheduleBuilder.cronSchedule(prpm[5]).withMisfireHandlingInstructionFireAndProceed()) //Configuración formato cron para determinar el tiempo de ejecución de la tarea
                        .build();
                try {
                    sched.scheduleJob(demoJob, trigger);

                } catch (SchedulerException e) {
                    txALog.append(e + "\n");
                }
            }
        } catch (SchedulerException ex) {
            txALog.append(ex + "\n");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("bandahmPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Prpm p ORDER BY p.estaId.estaId, p.copaId.copaId");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        prpmIdLabel = new javax.swing.JLabel();
        copaIdLabel = new javax.swing.JLabel();
        estaIdLabel = new javax.swing.JLabel();
        cfgesIdLabel = new javax.swing.JLabel();
        coparesuLabel = new javax.swing.JLabel();
        prpmoperLabel = new javax.swing.JLabel();
        prpmdeciLabel = new javax.swing.JLabel();
        prpminteLabel = new javax.swing.JLabel();
        prpmhorcLabel = new javax.swing.JLabel();
        prpmpasoLabel = new javax.swing.JLabel();
        prpmporcLabel = new javax.swing.JLabel();
        prpmfnteLabel = new javax.swing.JLabel();
        prpmdestLabel = new javax.swing.JLabel();
        prpmestaLabel = new javax.swing.JLabel();
        prpmIdField = new javax.swing.JTextField();
        copaIdField = new javax.swing.JTextField();
        estaIdField = new javax.swing.JTextField();
        cfgesIdField = new javax.swing.JTextField();
        coparesuField = new javax.swing.JTextField();
        prpmoperField = new javax.swing.JTextField();
        prpmdeciField = new javax.swing.JTextField();
        prpminteField = new javax.swing.JTextField();
        prpmhorcField = new javax.swing.JTextField();
        prpmpasoField = new javax.swing.JTextField();
        prpmporcField = new javax.swing.JTextField();
        prpmfnteField = new javax.swing.JTextField();
        prpmdestField = new javax.swing.JTextField();
        prpmestaField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txALog = new javax.swing.JTextArea();
        btnEjecutarScheduler = new javax.swing.JButton();
        btnStandByScheduler = new javax.swing.JButton();
        btnPararScheduler = new javax.swing.JButton();
        btnAgendarTareas = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmId}"));
        columnBinding.setColumnName("Prpm Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${copaId.parmId.parmdesc} ${copaId.umepId.umepsimb} ${copaId.estdId.estddesc} ${copaId.inutId.inteId.intetiemp} ${copaId.inutId.umetId.umetnomb}"));
        columnBinding.setColumnName("Copa Id");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${estaId.estacodi} ${estaId.estanomb}"));
        columnBinding.setColumnName("Esta Id");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cfgesId.sensId.inhmId.inhmnomb} ${cfgesId.sensId.inhmId.marcId.marcnomb} ${cfgesId.sensId.inhmId.inhmmode} ${cfgesId.sensId.inhmId.inhmseri}"));
        columnBinding.setColumnName("Cfges Id");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${coparesu.parmId.parmdesc} ${coparesu.umepId.umepsimb} ${coparesu.estdId.estddesc} ${coparesu.inutId.inteId.intetiemp} ${coparesu.inutId.umetId.umetnomb}"));
        columnBinding.setColumnName("Coparesu");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmoper}"));
        columnBinding.setColumnName("Prpmoper");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmdeci}"));
        columnBinding.setColumnName("Prpmdeci");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpminte}"));
        columnBinding.setColumnName("Prpminte");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmhorc}"));
        columnBinding.setColumnName("Prpmhorc");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmpaso}"));
        columnBinding.setColumnName("Prpmpaso");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmporc}"));
        columnBinding.setColumnName("Prpmporc");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmfnte}"));
        columnBinding.setColumnName("Prpmfnte");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmdest}"));
        columnBinding.setColumnName("Prpmdest");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prpmesta}"));
        columnBinding.setColumnName("Prpmesta");
        columnBinding.setColumnClass(Boolean.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);

        prpmIdLabel.setText("Prpm Id:");

        copaIdLabel.setText("Copa Id:");

        estaIdLabel.setText("Esta Id:");

        cfgesIdLabel.setText("Cfges Id:");

        coparesuLabel.setText("Coparesu:");

        prpmoperLabel.setText("Prpmoper:");

        prpmdeciLabel.setText("Prpmdeci:");

        prpminteLabel.setText("Prpminte:");

        prpmhorcLabel.setText("Prpmhorc:");

        prpmpasoLabel.setText("Prpmpaso:");

        prpmporcLabel.setText("Prpmporc:");

        prpmfnteLabel.setText("Prpmfnte:");

        prpmdestLabel.setText("Prpmdest:");

        prpmestaLabel.setText("Prpmesta:");

        prpmIdField.setEditable(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmId}"), prpmIdField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmIdField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        copaIdField.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.copaId.parmId.parmdesc} ${selectedElement.copaId.umepId.umepsimb} ${selectedElement.copaId.estdId.estddesc} ${selectedElement.copaId.inutId.inteId.intetiemp} ${selectedElement.copaId.inutId.umetId.umetnomb}"), copaIdField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), copaIdField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        estaIdField.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.estaId.estacodi} ${selectedElement.estaId.estanomb}"), estaIdField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), estaIdField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        cfgesIdField.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.cfgesId.sensId.inhmId.inhmnomb} ${selectedElement.cfgesId.sensId.inhmId.marcId.marcnomb} ${selectedElement.cfgesId.sensId.inhmId.inhmmode} ${selectedElement.cfgesId.sensId.inhmId.inhmseri}"), cfgesIdField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), cfgesIdField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        coparesuField.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.coparesu.parmId.parmdesc} ${selectedElement.coparesu.umepId.umepsimb} ${selectedElement.coparesu.estdId.estddesc} ${selectedElement.coparesu.inutId.inteId.intetiemp} ${selectedElement.coparesu.inutId.umetId.umetnomb}"), coparesuField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), coparesuField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmoper}"), prpmoperField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmoperField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmdeci}"), prpmdeciField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmdeciField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpminte}"), prpminteField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpminteField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmhorc}"), prpmhorcField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmhorcField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmpaso}"), prpmpasoField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmpasoField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmporc}"), prpmporcField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmporcField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmfnte}"), prpmfnteField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmfnteField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmdest}"), prpmdestField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmdestField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prpmesta}"), prpmestaField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prpmestaField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        saveButton.setText("Commit");
        saveButton.addActionListener(formListener);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(formListener);

        newButton.setText("Nuevo");
        newButton.addActionListener(formListener);

        deleteButton.setText("Eliminar");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        txALog.setColumns(20);
        txALog.setRows(5);
        jScrollPane1.setViewportView(txALog);

        btnEjecutarScheduler.setText("Ejecutar Scheduler");
        btnEjecutarScheduler.addActionListener(formListener);

        btnStandByScheduler.setText("StandBy Scheduler");
        btnStandByScheduler.addActionListener(formListener);

        btnPararScheduler.setText("Parar Scheduler");
        btnPararScheduler.addActionListener(formListener);

        btnAgendarTareas.setText("Agendar Tareas");
        btnAgendarTareas.addActionListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14)
                                .addComponent(refreshButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(prpmIdLabel)
                                    .addComponent(copaIdLabel)
                                    .addComponent(estaIdLabel)
                                    .addComponent(cfgesIdLabel)
                                    .addComponent(coparesuLabel)
                                    .addComponent(prpmoperLabel)
                                    .addComponent(prpmdeciLabel)
                                    .addComponent(prpminteLabel)
                                    .addComponent(prpmpasoLabel)
                                    .addComponent(prpmhorcLabel)
                                    .addComponent(prpmporcLabel)
                                    .addComponent(prpmfnteLabel)
                                    .addComponent(prpmdestLabel)
                                    .addComponent(prpmestaLabel))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(prpmIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(copaIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(estaIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cfgesIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(coparesuField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmoperField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmdeciField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpminteField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmpasoField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmhorcField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmporcField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmfnteField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmdestField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(prpmestaField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnEjecutarScheduler, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnStandByScheduler, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnPararScheduler, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnAgendarTareas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(masterScrollPane)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(prpmIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(prpmIdLabel)
                                .addComponent(btnEjecutarScheduler)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(copaIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(copaIdLabel)
                            .addComponent(btnStandByScheduler))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(estaIdLabel)
                            .addComponent(estaIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnPararScheduler))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cfgesIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cfgesIdLabel)
                            .addComponent(btnAgendarTareas))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(coparesuField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(coparesuLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmoperField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmoperLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmdeciField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmdeciLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpminteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpminteLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmpasoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmpasoLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmhorcLabel)
                            .addComponent(prpmhorcField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmporcField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmporcLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmfnteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmfnteLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmdestField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmdestLabel))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prpmestaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmestaLabel)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newButton)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(refreshButton)
                        .addComponent(saveButton)
                        .addComponent(deleteButton)))
                .addGap(6, 6, 6))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == saveButton) {
                Procesador.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                Procesador.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                Procesador.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton) {
                Procesador.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == btnEjecutarScheduler) {
                Procesador.this.btnEjecutarSchedulerActionPerformed(evt);
            }
            else if (evt.getSource() == btnStandByScheduler) {
                Procesador.this.btnStandBySchedulerActionPerformed(evt);
            }
            else if (evt.getSource() == btnPararScheduler) {
                Procesador.this.btnPararSchedulerActionPerformed(evt);
            }
            else if (evt.getSource() == btnAgendarTareas) {
                Procesador.this.btnAgendarTareasActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<Prpm> toRemove = new ArrayList<Prpm>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            try {
                Prpm p = list.get(masterTable.convertRowIndexToModel(selected[idx]));
                toRemove.add(p);
                entityManager.remove(p);
                sched.deleteJob(org.quartz.JobKey.jobKey("Proceso"+p.getPrpmId(), "group"));
            } catch (SchedulerException ex) {
                Logger.getLogger(Procesador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        list.removeAll(toRemove);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
//        Prpm p = new Prpm();
//        entityManager.persist(p);
//        list.add(p);
//        int row = list.size() - 1;
//        masterTable.setRowSelectionInterval(row, row);
//        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        NuevoProceso nuevoJFrame = new NuevoProceso(this,sched);
        nuevoJFrame.setVisible(true);
    }//GEN-LAST:event_newButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<Prpm> merged = new ArrayList<Prpm>(list.size());
            for (Prpm p : list) {
                merged.add(entityManager.merge(p));
            }
            list.clear();
            list.addAll(merged);
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void btnEjecutarSchedulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEjecutarSchedulerActionPerformed
        try {
            // TODO add your handling code here:
            sched.start();
            txALog.append("RUNNING SCHEDULER...\n");
        } catch (SchedulerException ex) {
//            Logger.getLogger(Procesador.class.getName()).log(Level.SEVERE, null, )
            txALog.append(ex + "\n");
        } catch (NullPointerException ex) {
            txALog.append(ex + ": MUST SCHEDULE JOBS FIRST\n");
        }
    }//GEN-LAST:event_btnEjecutarSchedulerActionPerformed

    private void btnStandBySchedulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStandBySchedulerActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            sched.standby();
            txALog.append("STANDBY SCHEDULER...\n");
        } catch (SchedulerException ex) {
            txALog.append(ex + "\n");
        } catch (NullPointerException ex) {
            txALog.append(ex + ": MUST SCHEDULE JOBS FIRST\n");
        }
    }//GEN-LAST:event_btnStandBySchedulerActionPerformed

    private void btnPararSchedulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPararSchedulerActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
//            sched

            Set<JobKey> llaves = sf.getScheduler().getJobKeys(GroupMatcher.jobGroupEquals("group"));
            Iterator it = llaves.iterator();
            while (it.hasNext()) {
                JobKey aux = (JobKey) it.next();
                sched.interrupt(aux);
                txALog.append(aux.getName());
            }
            sched.shutdown();
            txALog.append("SCHEDULER STOPPED...RESCHEDULE JOBS\n");
        } catch (SchedulerException ex) {
            txALog.append(ex + "\n");
        } catch (NullPointerException ex) {
            txALog.append(ex + ": MUST SCHEDULE JOBS FIRST\n");
        }
    }//GEN-LAST:event_btnPararSchedulerActionPerformed

    private void btnAgendarTareasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgendarTareasActionPerformed
        // TODO add your handling code here:
        agendarTareas();
        txALog.append("JOBS SCHEDULED...READY TO RUN SCHEDULER\n");

    }//GEN-LAST:event_btnAgendarTareasActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgendarTareas;
    private javax.swing.JButton btnEjecutarScheduler;
    private javax.swing.JButton btnPararScheduler;
    private javax.swing.JButton btnStandByScheduler;
    private javax.swing.JTextField cfgesIdField;
    private javax.swing.JLabel cfgesIdLabel;
    private javax.swing.JTextField copaIdField;
    private javax.swing.JLabel copaIdLabel;
    private javax.swing.JTextField coparesuField;
    private javax.swing.JLabel coparesuLabel;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField estaIdField;
    private javax.swing.JLabel estaIdLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<Prpm> list;/*
    private java.util.List<gob.inamhi.gui.Prpm> list;
    */
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.swing.JTextField prpmIdField;
    private javax.swing.JLabel prpmIdLabel;
    private javax.swing.JTextField prpmdeciField;
    private javax.swing.JLabel prpmdeciLabel;
    private javax.swing.JTextField prpmdestField;
    private javax.swing.JLabel prpmdestLabel;
    private javax.swing.JTextField prpmestaField;
    private javax.swing.JLabel prpmestaLabel;
    private javax.swing.JTextField prpmfnteField;
    private javax.swing.JLabel prpmfnteLabel;
    private javax.swing.JTextField prpmhorcField;
    private javax.swing.JLabel prpmhorcLabel;
    private javax.swing.JTextField prpminteField;
    private javax.swing.JLabel prpminteLabel;
    private javax.swing.JTextField prpmoperField;
    private javax.swing.JLabel prpmoperLabel;
    private javax.swing.JTextField prpmpasoField;
    private javax.swing.JLabel prpmpasoLabel;
    private javax.swing.JTextField prpmporcField;
    private javax.swing.JLabel prpmporcLabel;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextArea txALog;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Procesador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Procesador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Procesador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Procesador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new Procesador());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public Query getQuery() {
        return query;
    }

    public List<Prpm> getList() {
        return list;
    }

    public Scheduler getSched() {
        return sched;
    }
    
    
    
    
    
    
}
